package com.edu.controller;


import com.edu.domain.Course;
import com.edu.domain.CourseVo;
import com.edu.domain.ResponseResult;
import com.edu.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController  // @ResponseBody + @Controller
@RequestMapping("/course")
@CrossOrigin("*")
public class CourseController {

    // 注入courseService
    @Autowired
    private CourseService courseService;


    /**
     * 查询课程信息&条件查询 接口
     * */
    @RequestMapping("/findAllCourse")
    public ResponseResult findAllCourse(@RequestBody CourseVo courseVo){

        List<Course> courseList = courseService.findAllCourse(courseVo);

        ResponseResult result = new ResponseResult(true,200,"响应成功",courseList);

        return result;
    }

    /*多条件课程列表查询
    * */
    @RequestMapping("/findCourseByCondition")
    public ResponseResult findCourseByCondition( @RequestBody CourseVo courseVo){

        // 调用service
        List<Course> list = courseService.findCourseByCondition(courseVo);

        ResponseResult responseResult = new ResponseResult(true, 200, "响应成功", list);
        return responseResult;
    }

    /*课程图片上传
    * */
    @RequestMapping("/courseUpload")
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        //1 判断接收到的上传文件是否为空
        if(file.isEmpty()){
            throw new RuntimeException();
        }

        // 2 获取项目部署路径
        // C:\apache-tomcat-8.5.56\webapps\ssm_web\
        String realPath = request.getServletContext().getRealPath("/");
        // C:\apache-tomcat-8.5.56\webapps\
        String substring = realPath.substring(0, realPath.indexOf("ssm_web"));

        //3.获取原文件名
        //lagou.jpg
        String originalFilename = file.getOriginalFilename();

        //4.生成新文件名
        //12345334.jpg
        String newFileName = System.currentTimeMillis() + originalFilename.substring(originalFilename.lastIndexOf("."));

        //5.上传文件

        String uploadPath = substring + "upload\\";
        File filePath = new File(uploadPath, newFileName);

        //如果目录不存在就创建目录
        if(!filePath.getParentFile().exists()){
            filePath.getParentFile().mkdirs();
            System.out.println("创建目录" + filePath);
        }

        //图片进行了真正的上传
        file.transferTo(filePath);

        //6.将文件名和文件路径返回，进行响应
        Map<String, String> map = new HashMap<>();
        map.put("fileName",newFileName);
        // "filePath": "http://localhost:8080/upload/1597112871741.JPG"
        map.put("filePath","http://localhost:8080/upload/" + newFileName);

        ResponseResult responseResult = new ResponseResult(true, 200, "图片上传成功", map);

        return responseResult;
    }

    /*
        新增课程信息以及讲师信息
        新增课程信息和修改课程信息要写在同一个方法中
     */
    @RequestMapping("/saveOrUpdateCourse")
    private ResponseResult saveOrUpdateCourse(@RequestBody CourseVo courseVo) throws InvocationTargetException, IllegalAccessException {

        if(courseVo.getId() == null){
            courseService.saveCourseOrTeacher(courseVo);
            ResponseResult responseResult = new ResponseResult(true, 200, "新增响应成功", null);
            return responseResult;
        }else {
            courseService.updateCourseOrTeacher(courseVo);
            ResponseResult responseResult = new ResponseResult(true, 200, "修改响应成功", null);
            return responseResult;
        }

    }

    /*  根据id查询具体的课程信息及关联的讲师信息
    * */
    @RequestMapping("/findCourseById")
    public ResponseResult findCourseById(Integer id) {
        try {
            CourseVo courseVo = courseService.findCourseById(id);
            ResponseResult result = new ResponseResult(true,200,"根据id查询课程信息成功",courseVo);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*课程状态变更
    * */
    @RequestMapping("/updateCourseStatus")
    public ResponseResult updateCourseStatus (Integer id,Integer status){

        // 调用service传递参数，完成课程状态的变更
        courseService.updateCourseStatus(id,status);

        // 响应数据
        HashMap<String, Object> map = new HashMap<>();

        map.put("status",status);

        ResponseResult responseResult = new ResponseResult(true, 200, "响应成功", map);

        return responseResult;
    }
}


